package com.jinlejia.erp.ui.common;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jinlejia.erp.R;
import com.tencent.wework.api.IWWAPI;
import com.tencent.wework.api.WWAPIFactory;
import com.tencent.wework.api.model.WWMediaLink;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;


/**
 */
public class ShareDialog extends AlertDialog implements View.OnClickListener {

    private String shareTitle;
    private String shareTxt;
    private String thumb;
    private String shareUrl;
    private Context context;
    private int icon;
    private String Url_icon;
    private String smsBody;
    private String smsTo;

    private static final String APPID = "ww3e7842fda73fee01";
    private static final String AGENTID = "1000013";
    private static final String SCHEMA = "wwauth3e7842fda73fee01000013";
    IWWAPI iwwapi;
    private boolean hiden_sms = false;

    public ShareDialog(Context context) {
        super(context);
    }

    public ShareDialog(Context _context, String _subject,
                       String _content,String _smsBody,String _smsTo, String _thumb, int _icon, String _url,String url_icon) {
        super(_context);
        this.context = _context;
        this.shareTitle = _subject;
        this.shareTxt = _content;
        if (_thumb.toLowerCase().endsWith(".jpg") || _thumb.toLowerCase().endsWith(".png"))
            this.thumb = _thumb;
        this.shareUrl = _url;
        this.icon = _icon;
        this.smsBody = _smsBody;
        this.smsTo = _smsTo;
        this.Url_icon = url_icon;

        iwwapi = WWAPIFactory.createWWAPI(context);
        iwwapi.registerApp(SCHEMA);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
        window.setContentView(R.layout.share_dialog);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //取消
        TextView cancle = window.findViewById(R.id.id_share_cancle);
        //企业微信
        TextView workWechat = window.findViewById(R.id.id_work_wechat);
        //微信
        TextView wechat = window.findViewById(R.id.id_share_wechat);
        //朋友圈
        TextView frends = window.findViewById(R.id.id_share_frends);
        //QQ
        TextView qq = window.findViewById(R.id.id_share_qq);
        //短信
        TextView sms = window.findViewById(R.id.id_share_sms);

        cancle.setOnClickListener(this);
        wechat.setOnClickListener(this);
        frends.setOnClickListener(this);
        sms.setOnClickListener(this);
        qq.setOnClickListener(this);
        workWechat.setOnClickListener(this);
        if( hiden_sms ){
            workWechat.setVisibility(View.GONE);
            sms.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        iwwapi.unregisterApp();
    }
    public void hidenWorkWechatSms(){

        hiden_sms = true;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_share_cancle:
                //取消
                this.cancel();
                break;
            case R.id.id_share_wechat:
                //微信
                cancel();
                Wechat.ShareParams wechatSp = new Wechat.ShareParams();
                wechatSp.setTitle(shareTitle);
                wechatSp.setText(shareTxt);
                wechatSp.setShareType(Platform.SHARE_WEBPAGE);
                wechatSp.setUrl(shareUrl);
                if (!TextUtils.isEmpty(thumb)) {
                    wechatSp.setImageUrl(thumb);
                }else {
                    wechatSp.setImageUrl(Url_icon);
                }
                Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
                wechat.setPlatformActionListener(new OneKeyShareCallback());
                wechat.share(wechatSp);
                break;
            case R.id.id_share_frends:
                //朋友圈
                cancel();
                WechatMoments.ShareParams momentsSp = new WechatMoments.ShareParams();
                momentsSp.setTitle(shareTitle);
                momentsSp.setText(shareTxt);
                if (!TextUtils.isEmpty(thumb)) {
                    momentsSp.setImageUrl(thumb);
                }else {
                    momentsSp.setImageUrl(Url_icon);
                }
                momentsSp.setUrl(shareUrl);
                // 设置分享类型为网页
                momentsSp.setShareType (Platform.SHARE_WEBPAGE);
                Platform wechatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
                wechatMoments.setPlatformActionListener(new OneKeyShareCallback()); // 设置分享事件回调
                // 执行分享
                wechatMoments.share(momentsSp);
                break;
            case R.id.id_share_sms:
                //短信
                Uri smsToUri = Uri.parse("smsto:".concat(smsTo));
                Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
                intent.putExtra("sms_body", smsBody);
                context.startActivity(intent);
                cancel();
                break;
            case R.id.id_share_qq:
                //QQ
                cancel();
                QQ.ShareParams qqSp = new QQ.ShareParams();
                qqSp.setTitle(shareTitle);
                qqSp.setTitleUrl(shareUrl);
                qqSp.setText(shareTxt);
                qqSp.setUrl(shareUrl);
                if (!TextUtils.isEmpty(thumb)) {
                    qqSp.setImageUrl(thumb);
                }else {
                    qqSp.setImageUrl(Url_icon);
                }
                Platform qq = ShareSDK.getPlatform(QQ.NAME);
                qq.setPlatformActionListener(new OneKeyShareCallback());
                qq.share(qqSp);
                break;
            case R.id.id_work_wechat:
                cancel();
                WWMediaLink link = new WWMediaLink();
                link.thumbUrl = thumb;
                link.webpageUrl = shareUrl;
                link.title = shareTitle;
                link.description = shareTxt;
                link.appPkg = context.getPackageName();
                link.appName = "InHome";
                link.appId = APPID;
                link.agentId = AGENTID;
                iwwapi.sendMessage(link);
                break;
            default:
                break;
        }
    }

    static class OneKeyShareCallback implements PlatformActionListener {

        @Override
        public void onError(Platform platform, int action, Throwable t) {
            t.printStackTrace();
        }

        @Override
        public void onComplete(Platform platform, int action,
                               HashMap<String, Object> arg2) {
        }

        @Override
        public void onCancel(Platform platform, int action) {
        }

    }
}
